package com.dididoctor.patient.Activity.Doctor.Doctordetail;

import android.content.Context;
import android.util.Log;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.Http.MyHttpResponseHandler;
import com.dididoctor.patient.MV.BasePresenter;
import com.dididoctor.patient.MV.BusinessClient;
import com.dididoctor.patient.MV.Response;
import com.dididoctor.patient.Utils.Util;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/11.
 */
public class DoctorDetailPresenter extends BasePresenter {

    private DoctorDetailView view;

    public DoctorDetailPresenter(Context context, DoctorDetailView view) {
        super(context, view);
        this.view = view;
    }

    /**
     * 获取医生详情
     *
     * @param hospitalId
     * @param longitude
     * @param latitude
     */
    void getdoctordetail(String hospitalId, String longitude, String latitude) {
        RequestParams params = new RequestParams();
        params.put("hospitalId", hospitalId);
        params.put("longitude", longitude);
        params.put("latitude", latitude);
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        Log.d("aaaa", ConstantValue.doctorDetail + "?hospitalId=" + hospitalId + "&longitude=" + longitude + "&latitude=" + latitude + "&userId=" + Token.getId() + "&token=" + Token.getToken());
        BusinessClient.post(ConstantValue.doctorDetail, params, new MyHttpResponseHandler() {
            private List<DoctorEvaluate> evaluates = new ArrayList<DoctorEvaluate>();
            private List<Map<String, String>> maps = new ArrayList<Map<String, String>>();

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    DoctorDetail doctorDetail = new DoctorDetail();
                    Map<String, Object> doctormap = response.getMap("doctor");
                    doctorDetail.setOnline(Util.toString(doctormap.get("online")));
                    doctorDetail.setCallSwitch(Util.toString(doctormap.get("callSwitch")));
                    doctorDetail.setDoctorId(Util.toString(doctormap.get("doctorId")));
                    doctorDetail.setHospitalId(Util.toString(doctormap.get("hospitalId")));
                    doctorDetail.setHeadPic(Util.toString(doctormap.get("headPic")));
                    doctorDetail.setName(Util.toString(doctormap.get("name")));
                    doctorDetail.setHospital(Util.toString(doctormap.get("hospital")));
                    doctorDetail.setEvaluate(Util.toString(doctormap.get("evaluate")));
                    doctorDetail.setSignNum(Util.toString(doctormap.get("signNum")));
                    doctorDetail.setPosition(Util.toString(doctormap.get("position")));
                    doctorDetail.setPrice(Util.toString(doctormap.get("price")));
                    doctorDetail.setPhone(Util.toString(doctormap.get("phone")));
                    doctorDetail.setIsCollect(Util.toString(doctormap.get("isCollect")));
                    doctorDetail.setIsSign(Util.toString(doctormap.get("isSign")));
                    doctorDetail.setDistance(Util.toString(doctormap.get("distance")));
                    doctorDetail.setSign(Util.toString(doctormap.get("sign")));
                    doctorDetail.setAccId(Util.toString(doctormap.get("accId")));
                    maps = response.getListData("commentList");
                    for (Map<String, String> map : maps) {
                        DoctorEvaluate evaluate = new DoctorEvaluate();
                        evaluate.setUserId(Util.toString(map.get("userId")));
                        evaluate.setHeadPic(Util.toString(map.get("headPic")));
                        evaluate.setName(Util.toString(map.get("name")));
                        evaluate.setRemark(Util.toString(map.get("remark")));
                        evaluate.setStartTime(Util.toString(map.get("startTime")));
                        evaluate.setLevel(Util.toString(map.get("level")));
                        evaluates.add(evaluate);
                    }
                    view.getdoctordetail(doctorDetail, evaluates);
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getfail();
            }
        });

    }

    /**
     * 签约医生
     *
     * @param hospitalId
     */
    void signdoctor(String hospitalId, String payId) {
        RequestParams params = new RequestParams();
        params.put("hospitalId", hospitalId);
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("payId", payId);
        BusinessClient.post(ConstantValue.sign, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.signSucced();
                } else {
//                    view.showToastMessage(response.getMsg());
                    view.signfail();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.signfail();
            }
        });
    }

    /**
     * 收藏医生
     *
     * @param hospitalId
     */
    void collectdoctor(String hospitalId) {
        RequestParams params = new RequestParams();
        params.put("hospitalId", hospitalId);
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        BusinessClient.post(ConstantValue.collect, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.collectSucced();
                } else {
                    view.showToastMessage(response.getMsg());
                    view.collectfail();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.collectfail();
            }
        });
    }

    /**
     * 获取产品价格
     *
     * @param type
     */
    void getprdetail(String type) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("type", type);
        BusinessClient.post(ConstantValue.prdetail, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    Prdetail prdetail = new Prdetail();
                    prdetail.setPrdId(Util.toString((Double) response.getObject("prdId")));
                    prdetail.setName(Util.toString(response.getString("name")));
                    prdetail.setWebUrl(Util.toString(response.getString("webUrl")));
                    prdetail.setPrize((Double) response.getObject("prize") + "");
                    prdetail.setPrizes((Double) response.getObject("prize"));
                    prdetail.setMemo(Util.toString(response.getString("memo")));
                    view.getprdetail(prdetail);
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getprdetailfail();
            }
        });
    }

    /**
     * 下单产品
     *
     * @param prdId
     * @param price
     * @param ptype
     * @param type
     * @param recId
     * @param doctorId
     * @param hosId
     */
    void prdbuy(String prdId, String price, final String ptype, String type, String recId, String doctorId, String hosId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("prdId", prdId);
        params.put("price", price);
        params.put("ptype", ptype);
        params.put("type", type);
        params.put("recId", recId);
        params.put("doctorId", doctorId);
        params.put("hosId", hosId);
        params.put("payWay", "1");
        BusinessClient.post(ConstantValue.prdbuy, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
//                    "payId": "S101461907984",
//                            "sign": "hxzpuMfzOktmzRTxlfyKs8nrnsGaVVAz30l9itKsa0hRIiTEXDA4sTqCsk6to/pfOHYMvT5U29lRRaUXdEL0Xp6E9rBh42Ljy5qEbj1jlRz8jeyfNmLsyErz8rlY3YPBS/DzAH2o2V/RzHF10NqEbya2swc1edWvDtQxhiM9A7A=",
//                            "_input_charset": "utf-8",
//                            "subject": "签约医生",
//                            "total_fee": "0.01",
//                            "sign_type": "RSA",
//                            "service": "mobile.securitypay.pay",
//                            "notify_url": "http://192.168.1.181:8080/doctor/pay/alipayresult.do",
//                            "partner": "2088121649593163",
//                            "seller_id": "2088121649593163",
//                            "out_trade_no": "S101461907984",
//                            "payment_type": "1"


                    Prdbuy prdbuy = new Prdbuy();
                    prdbuy.setPayId(Util.toString(response.getString("payId")));
                    prdbuy.setSign(Util.toString(response.getString("sign")));
                    prdbuy.setNonceStr(Util.toString(response.getString("nonceStr")));
                    prdbuy.setDescr(Util.toString(response.getString("descr")));
                    prdbuy.setNotifyurl(Util.toString(response.getString("notifyurl")));
                    prdbuy.setTimeStamp(Util.toString(response.getString("timeStamp")));
                    prdbuy.setPartnerId(Util.toString(response.getString("partnerId")));
                    prdbuy.setPrepayId(Util.toString(response.getString("prepayId")));
                    prdbuy.set_input_charset(Util.toString(response.getString("_input_charset")));
                    prdbuy.setSubject(Util.toString(response.getString("subject")));
                    prdbuy.setTotal_fee(Util.toString(response.getString("total_fee")));
                    prdbuy.setSign_type(Util.toString(response.getString("sign_type")));
                    prdbuy.setService(Util.toString(response.getString("service")));
                    prdbuy.setNotify_url(Util.toString(response.getString("notify_url")));
                    prdbuy.setPartner(Util.toString(response.getString("partner")));
                    prdbuy.setSeller_id(Util.toString(response.getString("seller_id")));
                    prdbuy.setOut_trade_no(Util.toString(response.getString("out_trade_no")));
                    prdbuy.setPayment_type(Util.toString(response.getString("payment_type")));
                    prdbuy.setPrivateKey(Util.toString(response.getString("privateKey")));
                    view.prdbuy(prdbuy, ptype);
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.prdbuyfail();
            }
        });
    }


    public void getgoAndFrom(String hosId) {
//        hosId	String	是	医生所属医院id
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("hosId", hosId);
        BusinessClient.post(ConstantValue.medicalchatUrl, params, new MyHttpResponseHandler() {
            private String hasDoctor = "";//是否有签约医生1有0没有

            @Override
            public void onMyResponse(Response response) {
                if ("1".equals(response.getStatus())) {
                    String isCmp = response.getString("isCmp");
                    view.IntentInquiryActivity(isCmp);
                } else if ("2".equals(response.getStatus())) {
                    String recId = response.getString("recId");
                    view.IntentP2PActivity(recId, "2");
                } else if ("3".equals(response.getStatus())) {
                    String recId = response.getString("recId");
                    view.IntentP2PActivity(recId, "3");
                } else if ("4".equals(response.getStatus())) {
                    view.IntentInquiryActivity("1");
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getdoctorfail();
            }
        });
    }


    public void collectCancle(String hospitalId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("hospitalId", hospitalId);
        BusinessClient.post(ConstantValue.collectcancle, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.cancelcollect();
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.cancelcollectfail();
            }
        });
    }

    public void getCalling(String callee, String hospitalId, String doctorId, String recId, String disid) {
//        hosId	String	否	医生医院id reqType=2时用
//        doctorId	String	否	医生id reqType=2时用
//        recId	String	否	病历id reqType=1时用

        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("reqType", "2");
        params.put("accId", Token.getAccId());
        params.put("caller", Token.getPhone());
        params.put("callee", callee);
        params.put("hosId", hospitalId);
        params.put("doctorId", doctorId);
        params.put("recId", recId);
        params.put("diseId", disid);
        BusinessClient.post(ConstantValue.callingUrl, params, new MyHttpResponseHandler() {
            private String hasDoctor = "";//是否有签约医生1有0没有

            @Override
            public void onMyResponse(Response response) {
                String status = response.getStatus();
                if ("0".equals(status)) {
                    view.showToastMessage("拨打失败");
                } else if ("1".equals(status)) {
                    view.showToastMessage("正在拨打,请稍后");
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getdoctorfail();
            }
        });
    }

    public void paysuccess(String type, String payId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("type", type);
        params.put("payId", payId);
        BusinessClient.post(ConstantValue.paysuccess, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.paysuccess();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
            }
        });
    }
}
